﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WHC.OrderWater.Contract;
using WHC.OrderWater.Business;
using WHC.Hotel.Commons;

namespace WHC.Hotel.UI
{
    public partial class FrmEditYouhui : BaseForm
    {
        public string ID = string.Empty;

        public FrmEditYouhui()
        {
            InitializeComponent();
        }

        private void FrmEditYouhui_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(ID))
            {
                this.Text = "编辑 " + this.Text;
                YouhuiInfo info = BLLFactory<Youhui>.Instance.FindByID(ID);
                if (info != null)
                {
                    txtName.Text = info.Name;

                    txtProductName.Text = info.ProductName;
                    txtProductName.Tag = info.Product_ID;
                    txtQuantity.Text = info.Quantity.ToString();

                    txtGiftName.Text = info.GiftName;
                    txtGiftName.Tag = info.Gift_ID;
                    txtGiftQuantity.Text = info.GiftQuantity.ToString();

                    chkDisable.Checked = info.Disable;
                    txtTotalMoney.Text = info.TotalMoney.ToString("f2");
                    dtpStartDate.Value = info.StartDate;
                    dtpEndDate.Value = info.EndDate;

                    txtNote.Text = info.Note;
                    lblCreateDate.Text = info.LastUpdated.ToString();
                }

                this.btnOK.Enabled = Portal.gc.HasFunction("Youhui/Modify");
            }
            else
            {
                this.Text = "新建 " + this.Text;
                lblCreateDate.Text = DateTime.Now.ToString();
                this.btnOK.Enabled = Portal.gc.HasFunction("Youhui/Add");
            }
        }

        private void SetInfo(YouhuiInfo info)
        {
            info.Name = txtName.Text;
            info.ProductName = txtProductName.Text;
            info.Product_ID = txtProductName.Tag.ToString();
            info.Quantity = Convert.ToInt32(txtQuantity.Text);
            info.GiftName = txtGiftName.Text;
            info.Gift_ID = txtGiftName.Tag.ToString();
            info.GiftQuantity = Convert.ToInt32(txtGiftQuantity.Text);
            info.StartDate = dtpStartDate.Value;
            info.EndDate = dtpEndDate.Value;
            info.Disable = chkDisable.Checked;
            info.TotalMoney = Convert.ToDecimal(txtTotalMoney.Text);

            info.Note = txtNote.Text;
            info.LastUpdated = DateTime.Now;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            #region 验证输入
            if (this.txtName.Text.Trim().Length == 0)
            {
                MessageUtil.ShowTips("套餐名称不能为空");
                return;
            }
            else if (this.txtProductName.Text.Trim().Length == 0)
            {
                MessageUtil.ShowTips("购买产品名称不能为空");
                return;
            } 
            #endregion

            if (!string.IsNullOrEmpty(ID))
            {
                YouhuiInfo info = BLLFactory<Youhui>.Instance.FindByID(ID);
                if (info != null)
                {
                    SetInfo(info);

                    try
                    {
                        bool succeed = BLLFactory<Youhui>.Instance.Update(info, info.ID);
                        if (succeed)
                        {
                            MessageUtil.ShowTips("保存成功");
                            this.DialogResult = DialogResult.OK;
                        }
                    }
                    catch (Exception ex)
                    {
                        LogHelper.Error(ex);
                        MessageUtil.ShowError(ex.Message);
                    }
                }
            }
            else
            {
                YouhuiInfo info = new YouhuiInfo();
                SetInfo(info);
                info.ID = Guid.NewGuid().ToString();

                try
                {
                    bool succeed = BLLFactory<Youhui>.Instance.Insert(info);
                    if (succeed)
                    {
                        MessageUtil.ShowTips("保存成功");
                        this.DialogResult = DialogResult.OK;
                    }
                }
                catch (Exception ex)
                {
                    LogHelper.Error(ex);
                    MessageUtil.ShowError(ex.Message);
                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {

        }

        private void btnSelectProduct_Click(object sender, EventArgs e)
        {
            FrmSelectProduct dlg = new FrmSelectProduct();
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                this.txtProductName.Text = dlg.SelectedName;
                this.txtProductName.Tag = dlg.SelectedId;
            }
        }

        private void btnSelectGift_Click(object sender, EventArgs e)
        {
            FrmSelectProduct dlg = new FrmSelectProduct();
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                this.txtGiftName.Text = dlg.SelectedName;
                this.txtGiftName.Tag = dlg.SelectedId;
            }
        }
    }
}
